package com.jumper.study.front.controller;

import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.common.api.CommonPageResult;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.util.AdminUtil;
import com.jumper.study.db.model.Account;
import com.jumper.study.db.model.Lesson;
import com.jumper.study.front.dto.req.LessonBuyReq;
import com.jumper.study.front.dto.req.OrderCreateReq;
import com.jumper.study.front.dto.req.OrderSearchReq;
import com.jumper.study.front.dto.resp.OrderDetailResp;
import com.jumper.study.front.dto.resp.OrderItemResp;
import com.jumper.study.front.dto.vo.PageVO;
import com.jumper.study.front.service.LessonService;
import com.jumper.study.front.service.OrderService;
import com.jumper.study.front.service.StudentLessonService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController("frontOrderController")
@RequestMapping("/api/front/orders")
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    @Autowired
    private StudentLessonService studentLessonService;

    @Autowired
    private LessonService lessonService;

    @Autowired
    private AdminUtil adminUtil;

    /**
     * 查询订单
     * @param req
     * @return
     */
    @PostMapping("/")
    public CommonPageResult<List<OrderItemResp>> index(@RequestBody OrderSearchReq req) {
        PageVO page = new PageVO(req.getPage(), req.getLimit());
        Account account = adminUtil.getAccount();
        req.setAccountId(account.getId());
        List<OrderItemResp> resp = orderService.pageSearchList(page, req);
        return CommonPageResult.restPage(page.getPage(), page.getPageSize(), page.getTotal(), resp);
    }
    
    /**
     * 读取订单详情
     * @param orderno
     * @return
     */
    @GetMapping("/{orderno}")
    public CommonResult<OrderDetailResp> detail(@PathVariable String orderno) {
        Account account = adminUtil.getAccount();
        OrderDetailResp resp = orderService.detail(orderno, account.getId());
        if (resp == null) {
            return CommonResult.notFound("订单不存在");
        }

        return CommonResult.success(resp);
    }

    /**
     * 创建订单
     * @param req
     * @return
     */
    @PostMapping("/create")
    public CommonResult<String> buy(@RequestBody @Validated OrderCreateReq req) {
        Lesson lesson = lessonService.getById(req.getLessonId());
        if (lesson == null) {
            return CommonResult.notFound("课程不存在");
        }
        Account account = adminUtil.getAccount();
        req.setAccountId(account.getId());
        req.setPrice(lesson.getPrice());
        String orderno = orderService.create(req);
        return CommonResult.success(orderno);
    }
    
    /**
     * 取消订单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> cancel(@PathVariable Long id) {
        Account account = adminUtil.getAccount();
        boolean result = orderService.cancel(id, account.getId());
        return CommonResult.success(result);
    }

    /**
     * 课程是否已购买
     * @param req
     * @return
     */
    @PostMapping("/buyed")
    public CommonResult<Boolean> isBuyed(@RequestBody @Validated LessonBuyReq req) {
        Account account = adminUtil.getAccount();
        req.setAccountId(account.getId());
        boolean isBuyed = studentLessonService.isBuyed(req.getLessonId(), req.getAccountId());
        return CommonResult.success(isBuyed);
    }
    
}
